<template>
  <!-- 分层新增 -->
  <el-dialog v-dialogDrag :title="dialogTitle" :close-on-click-modal="false" class="dialogStyleSmall" :visible.sync="dialogVisible">
    <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
      <el-form-item label="层号" prop="layerNum">
        <label slot="label" v-html="`层&emsp;&emsp;号`" />
        <el-input v-model="formData.layerNum" :readonly="isEdit" size="medium" placeholder="请输入层号" />
      </el-form-item>
      <el-form-item label="名称" prop="setName">
        <label slot="label" v-html="`名&emsp;&emsp;称`" />
        <el-input v-model="formData.setName" size="medium" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="" prop="describe">
        <label slot="label" v-html="`描&emsp;&emsp;述`" />
        <el-input v-model="formData.describe" :rows="3" type="textarea" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button size="medium" :disabled="btnDisabled" type="primary" @click="submitForm">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
// api
import { addHierarchicaSetting, updateHierarchicaSetting, getHierarchicaSetting } from '@/api/projectManage/projectData/LayeredSettings'
// 字段校验
import { integer } from '@/utils/validate'
export default {
  name: 'DetailDialog',
  components: {},
  data() {
    return {
      /* 新增 start */
      isEdit: false,
      btnDisabled: false,
      dialogTitle: '', // 标题
      dialogVisible: false, // 对话框是否弹出显示
      formData: {
        id: '',
        proId: this.$store.state.basicInfo.proId,
        layerNum: '', // 层号
        setName: '', // 名称
        describe: '' // 描述
      },
      // from表单校验
      rules: {
        layerNum: [
          { required: true, message: '层号不能为空', trigger: 'blur' },
          { validator: integer, trigger: 'blur' }
        ],
        setName: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
      }
      /* 新增 end */
    }
  },
  methods: {
    //
    handleAdd(title, id) {
      this.dialogVisible = true
      this.dialogTitle = title
      if (id) {
        this.isEdit = true
        this.getDetailById(id)
      }
    },
    // 获取详情
    async getDetailById(id) {
      const { data } = await getHierarchicaSetting(id)
      this.formData = { ...this.formData, ...data }
    },
    // 弹框取消按钮
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
      this.emptyForm()
    },
    // 保存
    submitForm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.btnDisabled = true
          if (this.formData.id) {
            // 修改
            await updateHierarchicaSetting(this.formData)
            this.msgSuccess('修改成功')
          } else {
            // 新增
            await addHierarchicaSetting(this.formData)
            this.msgSuccess('保存成功')
          }
          this.cancel()
          this.$parent.getList()
        }
      })
    },
    // 清空弹出
    emptyForm() {
      this.resetForm('form')
      this.formData.id = ''
      this.isEdit = false
    }
  }
}
</script>

